package com.example.invoke.xsd;

import com.example.invoke.extend.bootstrap.CommonBootStrap;
import com.example.invoke.xsd.xml.CustomConfig;
import com.example.invoke.xsd.xml.ListenerConfig;
import com.example.invoke.xsd.xml.ServiceConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

/**
 * 把xsd读取到的数据，在生成bean后，自己再处理
 */
public class ConfigSpringImpl extends CustomConfig implements InitializingBean, ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void afterPropertiesSet() throws Exception {
        // 配置管理
        xsdBeanLoad();

        // 项目自动
        CommonBootStrap.setCustomConfig(this);
        String property = System.getProperty("RUN_CONSUMER", "false");
        if ("true".equals(property)) {
            CommonBootStrap bootStrap = new CommonBootStrap();
            bootStrap.start();
        }
    }

    /**
     * 自定义的bean，交由自己控制
     */
    private void xsdBeanLoad() {
        Map<String, ServiceConfig> serviceConfigMap = applicationContext.getBeansOfType(ServiceConfig.class);
        this.getServiceConfigList().addAll(serviceConfigMap.values());

        Map<String, ListenerConfig> listenerConfigMap = applicationContext.getBeansOfType(ListenerConfig.class);
        this.getListenerConfigList().addAll(listenerConfigMap.values());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        SpringContentUtil.setApplicationContext(applicationContext);
    }
}
